<?php
class ui_calendar_month extends ui_calendar
{
	function __construct($name=null)
	{
		global $ti;	
		$this->name = (is_null($name))?get_class($this):$name;
		$this->init(__FILE__,'table',array('cellspacing'=>0,'border'=>'100%','width'=>'100%','class'=>''));
		
		$this->year  = null;
		$this->month = null;
		
		$this->allow_quickclose = false;
		$this->add(new ui_tr());
		for ($i = 0; $i < 7; $i++)
		{
			$this->last->add(new ui_th());
			$this->last->last->text('Day '.$i);
		}
	}
	
	function pre_render()
	{
		global $ti;
		if(is_null($this->year))
		{
			$this->year = $ti->config['var.date']['year'];
		}
		if(is_null($this->month))
		{
			$this->month = $ti->config['var.date']['mon'];
		}
		#$ti->load_library('libdate');
		$days = ti_date::days_in_month($this->year,$this->month);
		$start_date = ti_date::cal_start_day($this->year,$this->month);
		$end_date =  ti_date::cal_end_day($this->year,$this->month);
		$weeks = ceil((($end_date - $start_date) / 86400) / 7);
		for ($i = 0; $i < $weeks; $i++)
		{
			$this->add(new ui_calendar_month_row());
			$this->last->set('id',$this->name.'_'.$i);
			for ($j = 0; $j < 7; $j++)
			{
				$this->last->add(new ui_calendar_month_cell());
				$this->last->last->set('id',$this->name.'_'.$i.'x'.$j);
				$this->last->last->add(new ui_calendar_month_day_label());
				$this->last->last->last->set('id',$this->name.'_'.$i.'x'.$j.'_label');
				$this->last->last->label = $this->last->last->last;
				$this->last->last->label->text(date('M jS',$start_date + (($i * 7 * 86400) + ($j * 86400))));
			}
		}
		#$ti->log('seconds: '.($end_date - $start_date));
		#	$ti->log('weeks: '.$weeks);
	}
}
?>